{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# requests"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实例引入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('https://www.baidu.com/')\n",
    "print(type(response))\n",
    "print(response.status_code)\n",
    "print(type(response.text))\n",
    "print(response.text)\n",
    "print(response.cookies)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 各种请求方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "requests.post('http://httpbin.org/post')\n",
    "requests.put('http://httpbin.org/put')\n",
    "requests.delete('http://httpbin.org/delete')\n",
    "requests.head('http://httpbin.org/get')\n",
    "requests.options('http://httpbin.org/get')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 请求"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本GET请求"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 基本写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('http://httpbin.org/get')\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 带参数GET请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "response = requests.get(\"http://httpbin.org/get?name=germey&age=22\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "data = {\n",
    "    'name': 'germey',\n",
    "    'age': 22\n",
    "}\n",
    "response = requests.get(\"http://httpbin.org/get\", params=data)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 解析json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "response = requests.get(\"http://httpbin.org/get\")\n",
    "print(type(response.text))\n",
    "print(response.json())\n",
    "print(json.loads(response.text))\n",
    "print(type(response.json()))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 获取二进制数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get(\"https://github.com/favicon.ico\")\n",
    "print(type(response.text), type(response.content))\n",
    "print(response.text)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get(\"https://github.com/favicon.ico\")\n",
    "with open('favicon.ico', 'wb') as f:\n",
    "    f.write(response.content)\n",
    "    f.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 添加headers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get(\"https://www.zhihu.com/explore\")\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'\n",
    "}\n",
    "response = requests.get(\"https://www.zhihu.com/explore\", headers=headers)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 基本POST请求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "data = {'name': 'germey', 'age': '22'}\n",
    "response = requests.post(\"http://httpbin.org/post\", data=data)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "data = {'name': 'germey', 'age': '22'}\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'\n",
    "}\n",
    "response = requests.post(\"http://httpbin.org/post\", data=data, headers=headers)\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 响应"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## reponse属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('http://www.jianshu.com')\n",
    "print(type(response.status_code), response.status_code)\n",
    "print(type(response.headers), response.headers)\n",
    "print(type(response.cookies), response.cookies)\n",
    "print(type(response.url), response.url)\n",
    "print(type(response.history), response.history)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 状态码判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('http://www.jianshu.com/hello.html')\n",
    "exit() if not response.status_code == requests.codes.not_found else print('404 Not Found')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('http://www.jianshu.com')\n",
    "exit() if not response.status_code == 200 else print('Request Successfully')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "100: ('continue',),\n",
    "101: ('switching_protocols',),\n",
    "102: ('processing',),\n",
    "103: ('checkpoint',),\n",
    "122: ('uri_too_long', 'request_uri_too_long'),\n",
    "200: ('ok', 'okay', 'all_ok', 'all_okay', 'all_good', '\\\\o/', '✓'),\n",
    "201: ('created',),\n",
    "202: ('accepted',),\n",
    "203: ('non_authoritative_info', 'non_authoritative_information'),\n",
    "204: ('no_content',),\n",
    "205: ('reset_content', 'reset'),\n",
    "206: ('partial_content', 'partial'),\n",
    "207: ('multi_status', 'multiple_status', 'multi_stati', 'multiple_stati'),\n",
    "208: ('already_reported',),\n",
    "226: ('im_used',),\n",
    "\n",
    "# Redirection.\n",
    "300: ('multiple_choices',),\n",
    "301: ('moved_permanently', 'moved', '\\\\o-'),\n",
    "302: ('found',),\n",
    "303: ('see_other', 'other'),\n",
    "304: ('not_modified',),\n",
    "305: ('use_proxy',),\n",
    "306: ('switch_proxy',),\n",
    "307: ('temporary_redirect', 'temporary_moved', 'temporary'),\n",
    "308: ('permanent_redirect',\n",
    "      'resume_incomplete', 'resume',), # These 2 to be removed in 3.0\n",
    "\n",
    "# Client Error.\n",
    "400: ('bad_request', 'bad'),\n",
    "401: ('unauthorized',),\n",
    "402: ('payment_required', 'payment'),\n",
    "403: ('forbidden',),\n",
    "404: ('not_found', '-o-'),\n",
    "405: ('method_not_allowed', 'not_allowed'),\n",
    "406: ('not_acceptable',),\n",
    "407: ('proxy_authentication_required', 'proxy_auth', 'proxy_authentication'),\n",
    "408: ('request_timeout', 'timeout'),\n",
    "409: ('conflict',),\n",
    "410: ('gone',),\n",
    "411: ('length_required',),\n",
    "412: ('precondition_failed', 'precondition'),\n",
    "413: ('request_entity_too_large',),\n",
    "414: ('request_uri_too_large',),\n",
    "415: ('unsupported_media_type', 'unsupported_media', 'media_type'),\n",
    "416: ('requested_range_not_satisfiable', 'requested_range', 'range_not_satisfiable'),\n",
    "417: ('expectation_failed',),\n",
    "418: ('im_a_teapot', 'teapot', 'i_am_a_teapot'),\n",
    "421: ('misdirected_request',),\n",
    "422: ('unprocessable_entity', 'unprocessable'),\n",
    "423: ('locked',),\n",
    "424: ('failed_dependency', 'dependency'),\n",
    "425: ('unordered_collection', 'unordered'),\n",
    "426: ('upgrade_required', 'upgrade'),\n",
    "428: ('precondition_required', 'precondition'),\n",
    "429: ('too_many_requests', 'too_many'),\n",
    "431: ('header_fields_too_large', 'fields_too_large'),\n",
    "444: ('no_response', 'none'),\n",
    "449: ('retry_with', 'retry'),\n",
    "450: ('blocked_by_windows_parental_controls', 'parental_controls'),\n",
    "451: ('unavailable_for_legal_reasons', 'legal_reasons'),\n",
    "499: ('client_closed_request',),\n",
    "\n",
    "# Server Error.\n",
    "500: ('internal_server_error', 'server_error', '/o\\\\', '✗'),\n",
    "501: ('not_implemented',),\n",
    "502: ('bad_gateway',),\n",
    "503: ('service_unavailable', 'unavailable'),\n",
    "504: ('gateway_timeout',),\n",
    "505: ('http_version_not_supported', 'http_version'),\n",
    "506: ('variant_also_negotiates',),\n",
    "507: ('insufficient_storage',),\n",
    "509: ('bandwidth_limit_exceeded', 'bandwidth'),\n",
    "510: ('not_extended',),\n",
    "511: ('network_authentication_required', 'network_auth', 'network_authentication'),"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 高级操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 文件上传"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "files = {'file': open('favicon.ico', 'rb')}\n",
    "response = requests.post(\"http://httpbin.org/post\", files=files)\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 获取cookie"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get(\"https://www.baidu.com\")\n",
    "print(response.cookies)\n",
    "for key, value in response.cookies.items():\n",
    "    print(key + '=' + value)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 会话维持"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模拟登录"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "requests.get('http://httpbin.org/cookies/set/number/123456789')\n",
    "response = requests.get('http://httpbin.org/cookies')\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "s = requests.Session()\n",
    "s.get('http://httpbin.org/cookies/set/number/123456789')\n",
    "response = s.get('http://httpbin.org/cookies')\n",
    "print(response.text)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 证书验证"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('https://www.12306.cn')\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from requests.packages import urllib3\n",
    "urllib3.disable_warnings()\n",
    "response = requests.get('https://www.12306.cn', verify=False)\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "response = requests.get('https://www.12306.cn', cert=('/path/server.crt', '/path/key'))\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 代理设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "proxies = {\n",
    "  \"http\": \"http://127.0.0.1:9743\",\n",
    "  \"https\": \"https://127.0.0.1:9743\",\n",
    "}\n",
    "\n",
    "response = requests.get(\"https://www.taobao.com\", proxies=proxies)\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "proxies = {\n",
    "    \"http\": \"http://user:password@127.0.0.1:9743/\",\n",
    "}\n",
    "response = requests.get(\"https://www.taobao.com\", proxies=proxies)\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pip3 install 'requests[socks]'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "proxies = {\n",
    "    'http': 'socks5://127.0.0.1:9742',\n",
    "    'https': 'socks5://127.0.0.1:9742'\n",
    "}\n",
    "response = requests.get(\"https://www.taobao.com\", proxies=proxies)\n",
    "print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 超时设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from requests.exceptions import ReadTimeout\n",
    "try:\n",
    "    response = requests.get(\"http://httpbin.org/get\", timeout = 0.5)\n",
    "    print(response.status_code)\n",
    "except ReadTimeout:\n",
    "    print('Timeout')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 认证设置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "from requests.auth import HTTPBasicAuth\n",
    "\n",
    "r = requests.get('http://120.27.34.24:9001', auth=HTTPBasicAuth('user', '123'))\n",
    "print(r.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "r = requests.get('http://120.27.34.24:9001', auth=('user', '123'))\n",
    "print(r.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 异常处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Connection error\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from requests.exceptions import ReadTimeout, ConnectionError, RequestException\n",
    "try:\n",
    "    response = requests.get(\"http://httpbin.org/get\", timeout = 0.5)\n",
    "    print(response.status_code)\n",
    "except ReadTimeout:\n",
    "    print('Timeout')\n",
    "except ConnectionError:\n",
    "    print('Connection error')\n",
    "except RequestException:\n",
    "    print('Error')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
